﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;

namespace UdrinkWater_Project.Admin
{
    public partial class EditTimeEmp : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public DataTable GetData_Check_Status(string Emp)
        {
            DataTable dt = new DataTable();
            string sqltext = @"SELECT * FROM [TB_EmpCheckIn] WHERE Employee_ID='"+Emp+"' AND CheckIn_Date BETWEEN CONVERT(DATE,DATEADD(s,+1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+0,0))) AND CONVERT(DATE,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))) AND Status_Check=1";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];
        }
      
        protected void GV_ShowEditTime_HtmlEditFormCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditFormEventArgs e)
        {
            if (!GV_ShowEditTime.IsNewRowEditing)
            {
                object[] vals = (object[])GV_ShowEditTime.GetRowValues(GV_ShowEditTime.EditingRowVisibleIndex, new string[]
                        {
                            "CheckOut_time","CheckIn_Time","Status_Time","ID_Card","CheckIn_Date"
                        });

                ASPxComboBox Status = ((ASPxComboBox)GV_ShowEditTime.FindEditFormTemplateControl("CB_Status") as ASPxComboBox);
                ASPxTimeEdit Time_In = ((ASPxTimeEdit)GV_ShowEditTime.FindEditFormTemplateControl("TimeEdit_In") as ASPxTimeEdit);
                ASPxTimeEdit Time_Out = ((ASPxTimeEdit)GV_ShowEditTime.FindEditFormTemplateControl("TimeEdit_Out") as ASPxTimeEdit);
                ASPxLabel lb_IDTime = ((ASPxLabel)GV_ShowEditTime.FindEditFormTemplateControl("lb_IDTime") as ASPxLabel);
                ASPxLabel lb_Date = ((ASPxLabel)GV_ShowEditTime.FindEditFormTemplateControl("lb_Date") as ASPxLabel);

                lb_Date.Text = vals[4].ToString();
                lb_IDTime.Text = vals[3].ToString();

                if (vals[2].ToString() == "")
                {
                    vals[2] = 0;
                    string DateNow = DateTime.Now.ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"));
                    Status.Value = vals[2];
                    Time_In.Value = Convert.ToDateTime(DateNow+" "+vals[1]);
                    Time_Out.Value = Convert.ToDateTime(DateNow + " " + vals[0]);
                }
                else
                {
                    vals[2] = 1;
                    Time_In.ReadOnly = true;
                    Time_Out.ReadOnly = true;
                    string DateNow = DateTime.Now.ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"));
                    Status.Value = vals[2];
                    Time_In.Value = Convert.ToDateTime(DateNow + " " + vals[1]);
                    Time_Out.Value = Convert.ToDateTime(DateNow + " " + vals[0]);
                    
                   
                }
               
                

            }
        }

        protected void GV_ShowEditTime_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            ASPxComboBox Status = ((ASPxComboBox)GV_ShowEditTime.FindEditFormTemplateControl("CB_Status") as ASPxComboBox);
            ASPxLabel lb_IDTime = ((ASPxLabel)GV_ShowEditTime.FindEditFormTemplateControl("lb_IDTime") as ASPxLabel);
            ASPxLabel lb_Date = ((ASPxLabel)GV_ShowEditTime.FindEditFormTemplateControl("lb_Date") as ASPxLabel);
            string DateNow = Convert.ToDateTime(lb_Date.Text).ToString("yyyy-MM-dd", new System.Globalization.CultureInfo("en-US"));

            if (Status.SelectedIndex==0)
            {
                e.NewValues["CheckIn_Time"] = ((ASPxTimeEdit)GV_ShowEditTime.FindEditFormTemplateControl("TimeEdit_In")).Value;
                e.NewValues["CheckOut_time"] = ((ASPxTimeEdit)GV_ShowEditTime.FindEditFormTemplateControl("TimeEdit_Out")).Value;
                string Status_Check =null;
                string TimeIn = e.NewValues["CheckIn_Time"].ToString();
                string TimeOut = e.NewValues["CheckOut_time"].ToString();
                Update_CheckIn(lb_IDTime.Text, DateNow, TimeIn, TimeOut, Status_Check);
                
            }
            else
            {
                DataTable dt = new DataTable();
                dt = GetData_Check_Status(lb_IDTime.Text);
                if (dt.Rows.Count < 2)
                {
                    string Status_Check ="1";
                    string TimeIn = null;
                    string TimeOut = null;
                    Update_CheckInEdit(lb_IDTime.Text, DateNow, TimeIn, TimeOut, Status_Check);
                }
                else
                {
                    string Status_Check = "2";
                    string TimeIn = null;
                    string TimeOut = null;
                    Update_CheckInEdit(lb_IDTime.Text, DateNow, TimeIn, TimeOut, Status_Check);
                }
            }

            
        }
        public static object GetDataValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }

            return value;
        }
        public void Update_CheckIn(string Emp, string Time,string Checkin,string CheckOut,string Status)
        {
            string sqltext = @"UPDATE TB_EmpCheckIn SET CheckIn_Time=@CheckIn,Status_Check=@Status  
                               WHERE (Employee_ID =@EmployeeID) AND (CheckIn_Date=@Datetime);

                               UPDATE TB_EmpCheckOut SET CheckOut_time=@CheckOut  
                               WHERE (Employee_ID =@EmployeeID) AND (CheckOut_Date=@Datetime);";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@EmployeeID",GetDataValue(Emp));
            sqlcomm.Parameters.AddWithValue("@CheckIn", GetDataValue(Convert.ToDateTime(Checkin)));
            sqlcomm.Parameters.AddWithValue("@CheckOut", GetDataValue(Convert.ToDateTime(CheckOut)));
            sqlcomm.Parameters.AddWithValue("@Datetime", GetDataValue(Time));
            sqlcomm.Parameters.AddWithValue("@Status", GetDataValue(Status));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public void Update_CheckInEdit(string Emp, string Time, string Checkin, string CheckOut, string Status)
        {
            string sqltext = @"UPDATE TB_EmpCheckIn SET CheckIn_Time=@CheckIn,Status_Check=@Status  
                               WHERE (Employee_ID =@EmployeeID) AND (CheckIn_Date=@Datetime);

                               UPDATE TB_EmpCheckOut SET CheckOut_time=@CheckOut  
                               WHERE (Employee_ID =@EmployeeID) AND (CheckOut_Date=@Datetime);";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@EmployeeID", GetDataValue(Emp));
            sqlcomm.Parameters.AddWithValue("@CheckIn", GetDataValue(Checkin));
            sqlcomm.Parameters.AddWithValue("@CheckOut", GetDataValue(CheckOut));
            sqlcomm.Parameters.AddWithValue("@Datetime", GetDataValue(Time));
            sqlcomm.Parameters.AddWithValue("@Status", GetDataValue(Status));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        protected void CB_Status_SelectedIndexChanged(object sender, EventArgs e)
        {
            ASPxComboBox CB_Status = (ASPxComboBox)sender;
            ASPxTimeEdit Time_In = ((ASPxTimeEdit)GV_ShowEditTime.FindEditFormTemplateControl("TimeEdit_In") as ASPxTimeEdit);
            ASPxTimeEdit Time_Out = ((ASPxTimeEdit)GV_ShowEditTime.FindEditFormTemplateControl("TimeEdit_Out") as ASPxTimeEdit);
            if (CB_Status.SelectedIndex==1)
            {
                Time_In.ReadOnly = true;
                Time_Out.ReadOnly = true;
                Time_In.Value = "";
                Time_Out.Value = "";
            }
            else
            {
                Time_In.ReadOnly = false;
                Time_Out.ReadOnly = false;
            }
        }

        protected void GV_EditTimeOver_HtmlEditFormCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditFormEventArgs e)
        {
            if (!GV_EditTimeOver.IsNewRowEditing)
            {
                object[] vals = (object[])GV_EditTimeOver.GetRowValues(GV_EditTimeOver.EditingRowVisibleIndex, new string[]
                        {
                            "CheckIN_Date","CheckIn_Time","CheckOut_Time","ID_Card"
                        });

                ASPxTimeEdit Time_In = ((ASPxTimeEdit)GV_EditTimeOver.FindEditFormTemplateControl("TimeEditOverIn") as ASPxTimeEdit);
                ASPxTimeEdit Time_Out = ((ASPxTimeEdit)GV_EditTimeOver.FindEditFormTemplateControl("TimeEditOverOut") as ASPxTimeEdit);
                ASPxLabel lb_IDTime = ((ASPxLabel)GV_EditTimeOver.FindEditFormTemplateControl("lb_IDOver") as ASPxLabel);
                ASPxLabel lb_Date = ((ASPxLabel)GV_EditTimeOver.FindEditFormTemplateControl("lb_DateOver") as ASPxLabel);

                    string DateNow = DateTime.Now.ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"));
                    Time_In.Value = Convert.ToDateTime(DateNow + " " + vals[1]);
                    Time_Out.Value = Convert.ToDateTime(DateNow + " " + vals[2]);
                    lb_Date.Text = vals[0].ToString();
                    lb_IDTime.Text = vals[3].ToString();

            }
        }

        protected void GV_EditTimeOver_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            ASPxLabel lb_IDTime = ((ASPxLabel)GV_EditTimeOver.FindEditFormTemplateControl("lb_IDOver") as ASPxLabel);
            ASPxLabel lb_Date = ((ASPxLabel)GV_EditTimeOver.FindEditFormTemplateControl("lb_DateOver") as ASPxLabel);

            string DateNow = Convert.ToDateTime(lb_Date.Text).ToString("yyyy-MM-dd", new System.Globalization.CultureInfo("en-US"));
            e.NewValues["CheckIn_Time"] = ((ASPxTimeEdit)GV_EditTimeOver.FindEditFormTemplateControl("TimeEditOverIn")).Value;
            e.NewValues["CheckOut_Time"] = ((ASPxTimeEdit)GV_EditTimeOver.FindEditFormTemplateControl("TimeEditOverOut")).Value;
            string TimeIn = e.NewValues["CheckIn_Time"].ToString();
            string TimeOut = e.NewValues["CheckOut_Time"].ToString();

            Update_OverTime(lb_IDTime.Text, DateNow, TimeIn, TimeOut);
        }
        public void Update_OverTime(string Emp, string Time, string Checkin, string CheckOut)
        {
            string sqltext = @"UPDATE TB_EmployeeOT_IN SET CheckIn_Time=@CheckIn  
                               WHERE (Employee_ID =@EmployeeID) AND (CheckIN_Date=@Datetime);

                               UPDATE TB_EmployeeOT_Out SET CheckOut_time=@CheckOut  
                               WHERE (Employee_ID =@EmployeeID) AND (CheckOut_Date=@Datetime);";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@EmployeeID", GetDataValue(Emp));
            sqlcomm.Parameters.AddWithValue("@CheckIn", GetDataValue(Convert.ToDateTime(Checkin)));
            sqlcomm.Parameters.AddWithValue("@CheckOut", GetDataValue(Convert.ToDateTime(CheckOut)));
            sqlcomm.Parameters.AddWithValue("@Datetime", GetDataValue(Time));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
    }
}